package dict_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type DictORM struct {
	CreatedAt   *time.Time
	Desc        string
	DictDetails []*DictDetailORM `gorm:"foreignkey:DictId;association_foreignkey:Id" atlas:"position:Id"`
	Id          uint64
	Name        string
	Status      bool
	Type        string
	UpdatedAt   *time.Time
}

// TableName overrides the default tablename generated by GORM
func (DictORM) TableName() string {
	return "dicts"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *Dict) ToORM(ctx context.Context) (DictORM, error) {
	to := DictORM{}
	var err error
	if prehook, ok := interface{}(m).(DictWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.Name = m.Name
	to.Type = m.Type
	to.Status = m.Status
	to.Desc = m.Desc
	for _, v := range m.DictDetails {
		if v != nil {
			if tempDictDetails, cErr := v.ToORM(ctx); cErr == nil {
				to.DictDetails = append(to.DictDetails, &tempDictDetails)
			} else {
				return to, cErr
			}
		} else {
			to.DictDetails = append(to.DictDetails, nil)
		}
	}
	for i, e := range to.DictDetails {
		e.Id = uint64(i)
	}
	if posthook, ok := interface{}(m).(DictWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *DictORM) ToPB(ctx context.Context) (Dict, error) {
	to := Dict{}
	var err error
	if prehook, ok := interface{}(m).(DictWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	to.Id = m.Id
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.Name = m.Name
	to.Type = m.Type
	to.Status = m.Status
	to.Desc = m.Desc
	for _, v := range m.DictDetails {
		if v != nil {
			if tempDictDetails, cErr := v.ToPB(ctx); cErr == nil {
				to.DictDetails = append(to.DictDetails, &tempDictDetails)
			} else {
				return to, cErr
			}
		} else {
			to.DictDetails = append(to.DictDetails, nil)
		}
	}
	if posthook, ok := interface{}(m).(DictWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type Dict the arg will be the target, the caller the one being converted from

// DictBeforeToORM called before default ToORM code
type DictWithBeforeToORM interface {
	BeforeToORM(context.Context, *DictORM) error
}

// DictAfterToORM called after default ToORM code
type DictWithAfterToORM interface {
	AfterToORM(context.Context, *DictORM) error
}

// DictBeforeToPB called before default ToPB code
type DictWithBeforeToPB interface {
	BeforeToPB(context.Context, *Dict) error
}

// DictAfterToPB called after default ToPB code
type DictWithAfterToPB interface {
	AfterToPB(context.Context, *Dict) error
}

// DefaultCreateDict executes a basic gorm create call
func DefaultCreateDict(ctx context.Context, in *Dict, db *gorm.DB) (*Dict, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type DictORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadDict(ctx context.Context, in *Dict, db *gorm.DB, fs *query.FieldSelection) (*Dict, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, fs, &DictORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	ormResponse := DictORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(DictORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db, fs); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type DictORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type DictORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB, *query.FieldSelection) (*gorm.DB, error)
}
type DictORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB, *query.FieldSelection) error
}

func DefaultDeleteDict(ctx context.Context, in *Dict, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&DictORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type DictORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeleteDictSet(ctx context.Context, in []*Dict, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&DictORM{})).(DictORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&DictORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&DictORM{})).(DictORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type DictORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*Dict, *gorm.DB) (*gorm.DB, error)
}
type DictORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*Dict, *gorm.DB) error
}

// DefaultStrictUpdateDict clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdateDict(ctx context.Context, in *Dict, db *gorm.DB) (*Dict, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdateDict")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &DictORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	filterDictDetails := DictDetailORM{}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	filterDictDetails.DictId = ormObj.Id
	if err = db.Where(filterDictDetails).Delete(DictDetailORM{}).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type DictORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type DictORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchDict executes a basic gorm update call with patch behavior
func DefaultPatchDict(ctx context.Context, in *Dict, updateMask *field_mask.FieldMask, db *gorm.DB) (*Dict, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj Dict
	var err error
	if hook, ok := interface{}(&pbObj).(DictWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadDict(ctx, &Dict{Id: in.GetId()}, db, nil)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(DictWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskDict(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(DictWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdateDict(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(DictWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type DictWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *Dict, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *Dict, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *Dict, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type DictWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *Dict, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetDict executes a bulk gorm update call with patch behavior
func DefaultPatchSetDict(ctx context.Context, objects []*Dict, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*Dict, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*Dict, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchDict(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskDict patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskDict(ctx context.Context, patchee *Dict, patcher *Dict, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*Dict, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"Name" {
			patchee.Name = patcher.Name
			continue
		}
		if f == prefix+"Type" {
			patchee.Type = patcher.Type
			continue
		}
		if f == prefix+"Status" {
			patchee.Status = patcher.Status
			continue
		}
		if f == prefix+"Desc" {
			patchee.Desc = patcher.Desc
			continue
		}
		if f == prefix+"DictDetails" {
			patchee.DictDetails = patcher.DictDetails
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListDict executes a gorm list call
func DefaultListDict(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*Dict, error) {
	in := Dict{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &DictORM{}, &Dict{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []DictORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(DictORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*Dict{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type DictORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type DictORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type DictORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]DictORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type DictServiceDefaultServer struct {
	DB *gorm.DB
}

// Create ...
func (m *DictServiceDefaultServer) Create(ctx context.Context, in *CreateDictRequest) (*CreateDictResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateDict(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateDictResponse{Result: res}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeCreate called before DefaultCreateDict in the default Create handler
type DictServiceDictWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterCreate called before DefaultCreateDict in the default Create handler
type DictServiceDictWithAfterCreate interface {
	AfterCreate(context.Context, *CreateDictResponse, *gorm.DB) error
}

// Read ...
func (m *DictServiceDefaultServer) Read(ctx context.Context, in *ReadDictRequest) (*ReadDictResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadDict(ctx, &Dict{Id: in.GetId()}, db, in.Fields)
	if err != nil {
		return nil, err
	}
	out := &ReadDictResponse{Result: res}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeRead called before DefaultReadDict in the default Read handler
type DictServiceDictWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterRead called before DefaultReadDict in the default Read handler
type DictServiceDictWithAfterRead interface {
	AfterRead(context.Context, *ReadDictResponse, *gorm.DB) error
}

// Update ...
func (m *DictServiceDefaultServer) Update(ctx context.Context, in *UpdateDictRequest) (*UpdateDictResponse, error) {
	var err error
	var res *Dict
	db := m.DB
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	if in.GetGerogeriGegege() == nil {
		res, err = DefaultStrictUpdateDict(ctx, in.GetPayload(), db)
	} else {
		res, err = DefaultPatchDict(ctx, in.GetPayload(), in.GetGerogeriGegege(), db)
	}
	if err != nil {
		return nil, err
	}
	out := &UpdateDictResponse{Result: res}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeUpdate called before DefaultUpdateDict in the default Update handler
type DictServiceDictWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterUpdate called before DefaultUpdateDict in the default Update handler
type DictServiceDictWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdateDictResponse, *gorm.DB) error
}

// UpdateSet ...
func (m *DictServiceDefaultServer) UpdateSet(ctx context.Context, in *UpdateSetDictRequest) (*UpdateSetDictResponse, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}

	db := m.DB

	if custom, ok := interface{}(in).(DictServiceDictWithBeforeUpdateSet); ok {
		var err error
		if db, err = custom.BeforeUpdateSet(ctx, db); err != nil {
			return nil, err
		}
	}

	res, err := DefaultPatchSetDict(ctx, in.GetObjects(), in.GetMasks(), db)
	if err != nil {
		return nil, err
	}

	out := &UpdateSetDictResponse{Results: res}

	if custom, ok := interface{}(in).(DictServiceDictWithAfterUpdateSet); ok {
		var err error
		if err = custom.AfterUpdateSet(ctx, out, db); err != nil {
			return nil, err
		}
	}

	return out, nil
}

// DictServiceDictWithBeforeUpdateSet called before DefaultUpdateSetDict in the default UpdateSet handler
type DictServiceDictWithBeforeUpdateSet interface {
	BeforeUpdateSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterUpdateSet called before DefaultUpdateSetDict in the default UpdateSet handler
type DictServiceDictWithAfterUpdateSet interface {
	AfterUpdateSet(context.Context, *UpdateSetDictResponse, *gorm.DB) error
}

// List ...
func (m *DictServiceDefaultServer) List(ctx context.Context, in *ListDictRequest) (*ListDictResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListDict(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListDictResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeList called before DefaultListDict in the default List handler
type DictServiceDictWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterList called before DefaultListDict in the default List handler
type DictServiceDictWithAfterList interface {
	AfterList(context.Context, *ListDictResponse, *gorm.DB) error
}

// Delete ...
func (m *DictServiceDefaultServer) Delete(ctx context.Context, in *DeleteDictRequest) (*DeleteDictResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteDict(ctx, &Dict{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteDictResponse{}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeDelete called before DefaultDeleteDict in the default Delete handler
type DictServiceDictWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterDelete called before DefaultDeleteDict in the default Delete handler
type DictServiceDictWithAfterDelete interface {
	AfterDelete(context.Context, *DeleteDictResponse, *gorm.DB) error
}

// DeleteSet ...
func (m *DictServiceDefaultServer) DeleteSet(ctx context.Context, in *DeletesDictRequest) (*DeleteDictResponse, error) {
	db := m.DB
	objs := []*Dict{}
	for _, id := range in.Ids {
		objs = append(objs, &Dict{Id: id})
	}
	if custom, ok := interface{}(in).(DictServiceDictWithBeforeDeleteSet); ok {
		var err error
		if db, err = custom.BeforeDeleteSet(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteDictSet(ctx, objs, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteDictResponse{}
	if custom, ok := interface{}(in).(DictServiceDictWithAfterDeleteSet); ok {
		var err error
		if err = custom.AfterDeleteSet(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// DictServiceDictWithBeforeDeleteSet called before DefaultDeleteSetDict in the default DeleteSet handler
type DictServiceDictWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, *gorm.DB) (*gorm.DB, error)
}

// DictServiceDictWithAfterDeleteSet called before DefaultDeleteSetDict in the default DeleteSet handler
type DictServiceDictWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, *DeleteDictResponse, *gorm.DB) error
}
